package cn.fscode.code.generator.controller;

import cn.fscode.code.generator.model.po.TemplateConfigPO;
import cn.fscode.code.generator.model.req.CopyTemplateReq;
import cn.fscode.code.generator.model.req.TemplateConfigReq;
import cn.fscode.code.generator.model.req.TemplateQueryReq;
import cn.fscode.code.generator.model.resp.TemplateConfigResp;
import cn.fscode.code.generator.model.resp.VelocityVariableResp;
import cn.fscode.code.generator.service.TemplateConfigService;
import cn.fscode.code.generator.utils.poi.ExcelUtil;
import cn.fscode.commons.core.response.Resp;
import cn.hutool.core.io.resource.ResourceUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.TypeReference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * @author shenguangyang
 */
@RestController
@Validated
@RequestMapping("/api/template")
@RequiredArgsConstructor
public class TemplateConfigController {
    private final TemplateConfigService templateConfigService;
    @GetMapping("/exportXls")
    public void exportXls(HttpServletResponse response, @Validated TemplateQueryReq req) {
        List<TemplateConfigPO> list = templateConfigService.list(req);
        ExcelUtil<TemplateConfigPO> util = new ExcelUtil<>(TemplateConfigPO.class);
        util.exportExcel(response, list, "模板");
    }

    @PostMapping("/importExcel")
    public Resp<?> importExcel(MultipartFile file,
                               @RequestParam(value = "groupId", required = false)
                               @NotNull(message = "请选择模板组") Long groupId) throws Exception {
        ExcelUtil<TemplateConfigPO> util = new ExcelUtil<>(TemplateConfigPO.class);
        List<TemplateConfigPO> userList = util.importExcel(file.getInputStream());
        String message = templateConfigService.importExcel(userList, groupId, true);
        return Resp.ok(message);
    }

    @GetMapping("/getById")
    public Resp<TemplateConfigPO> get(@RequestParam("id") Long id) {
        return Resp.ok(templateConfigService.getById(id));
    }

    @GetMapping("/listByGroupId")
    public Resp<List<TemplateConfigResp>> listByGroupId(@RequestParam("groupId") Long groupId) {
        return Resp.ok(templateConfigService.listByGroupId(groupId));
    }

    @GetMapping("/page")
    public Resp<IPage<TemplateConfigPO>> page(@Validated TemplateQueryReq req) {
        IPage<TemplateConfigPO> resp = templateConfigService.page(req);
        return Resp.ok(resp);
    }

    @PostMapping("/update")
    public Resp<?> update(@RequestBody @Validated TemplateConfigReq req) {
        templateConfigService.updateById(req);
        return Resp.ok();
    }

    @DeleteMapping("/delete")
    public Resp<?> delete(@RequestBody TemplateConfigPO templateConfigPO) {
        templateConfigService.deleteById(templateConfigPO);
        return Resp.ok();
    }

    @DeleteMapping("/deleteBatch")
    public Resp<?> deleteBatch(@RequestParam("ids") List<Long> ids) {
        templateConfigService.deleteBatchByIds(ids);
        return Resp.ok();
    }

    @PostMapping("/save")
    public Resp<?> save(@RequestBody @Validated TemplateConfigReq req) {
        templateConfigService.save(req);
        return Resp.ok();
    }

    @PostMapping("/copy")
    public Resp<?> copy(@RequestBody CopyTemplateReq req) {
        templateConfigService.copyById(req);
        return Resp.ok();
    }

    public List<VelocityVariableResp.Variable> readData(String fileName) {
        String content = ResourceUtil.readStr("json/velocity-variable/" + fileName + ".json", StandardCharsets.UTF_8);
        return JSON.parseObject(content, new TypeReference<List<VelocityVariableResp.Variable>>() {{}});
    }

    /**
     * 获取Velocity模板引擎变量
     */
    @GetMapping("/listVelocityVar")
    public Resp<List<VelocityVariableResp>> listVelocityVar() {
        List<VelocityVariableResp> respList = new ArrayList<>();
        List<VelocityVariableResp.Variable> javaJsonData= readData("java");
        List<VelocityVariableResp.Variable> csharpData = readData("csharp");

        VelocityVariableResp javaResp = VelocityVariableResp.builder()
                .tab("Java变量").key("java").varList(javaJsonData).build();
        respList.add(javaResp);

        VelocityVariableResp csharpResp = VelocityVariableResp.builder()
                .tab("C#变量").key("C#").varList(csharpData).build();
        respList.add(csharpResp);
        return Resp.ok(respList);
    }

}
